package com.haierCamera.customapplication.ui.plan;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.CompoundButton;
import android.widget.Toast;

import androidx.databinding.DataBindingUtil;

import com.haierCamera.customapplication.BaseActivity;
import com.haierCamera.customapplication.R;
import com.haierCamera.customapplication.databinding.HzklActivityDeviceMoreBinding;
import com.haierCamera.customapplication.ui.business.Business;
import com.lechange.opensdk.api.bean.BreathingLightStatus;
import com.lechange.opensdk.api.bean.FrameReverseStatus;

/**
 * Created by AdministratorZJ
 * on 2020/3/30.
 */
public class HZKLDeviceMoreActivity extends BaseActivity {
    private HzklActivityDeviceMoreBinding binding;
    private boolean IsClickLightSwitchBtn; // 是否主动修改动检开关（手势触发）
    private boolean isClickFrameBtn; // 是否主动修改动检开关（手势触发）
    private String deviceCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.hzkl_activity_device_more);
        setSupportActionBar(binding.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        initData();
    }

    private void initData() {
        // 获取通道的UUID
        deviceCode = getIntent().getStringExtra("deviceCode");
        binding.lightBtn.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (IsClickLightSwitchBtn) {
                setLightStatus(isChecked);
            }
        });
        binding.imageBtn.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isClickFrameBtn) {
                setFrameReverseStatus(isChecked);
            }
        });
        new Handler().postDelayed(this::getLightStatus,800);
        getFrameReverseStatus();
    }

    private void getLightStatus() {
        Business.getInstance().getLightStatus(deviceCode, new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what == 0) {
                    BreathingLightStatus.ResponseData data = ((BreathingLightStatus.Response) (((Business.RetObject) msg.obj).resp)).data;
                    if (data.status.equals("on")) {
                        IsClickLightSwitchBtn = false;
                        binding.lightBtn.setChecked(true);
                    } else {
                        IsClickLightSwitchBtn = false;
                        binding.lightBtn.setChecked(false);
                    }
                } else {
                    Toast.makeText(HZKLDeviceMoreActivity.this, ((Business.RetObject) msg.obj).mMsg, Toast.LENGTH_SHORT).show();
                }
                IsClickLightSwitchBtn = true;
            }
        });
    }

    private void setLightStatus(final boolean isChecked) {
        binding.lightBtn.setClickable(false);
        String stats;
        if (isChecked) {
            stats = "on";
        } else {
            stats = "off";
        }
        Business.getInstance().ModifyBreat(deviceCode, stats, new Handler() {
                    @Override
                    public void handleMessage(Message msg) {
                        if (msg.what == 0) {
                            Toast.makeText(HZKLDeviceMoreActivity.this, ((Business.RetObject) msg.obj).mMsg, Toast.LENGTH_SHORT).show();
                        } else {
                            IsClickLightSwitchBtn = false;
                            binding.lightBtn.setChecked(!isChecked);
                            Toast.makeText(HZKLDeviceMoreActivity.this, ((Business.RetObject) msg.obj).mMsg, Toast.LENGTH_SHORT).show();
                        }
                        IsClickLightSwitchBtn = true;
                        binding.lightBtn.setClickable(true);
                    }
                }
        );
    }


    private void getFrameReverseStatus() {
        if (null == deviceCode) {
            Toast.makeText(this, "ChannelInfo为null", Toast.LENGTH_SHORT).show();
            return;
        }
        Business.getInstance().getFrameReverseStatus(deviceCode, "0", new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what == 0) {
                    FrameReverseStatus.ResponseData data = ((FrameReverseStatus.Response) (((Business.RetObject) msg.obj).resp)).data;
                    if (data.direction.equals("reverse")) {
                        isClickFrameBtn = false;
                        binding.imageBtn.setChecked(true);
                    } else {
                        isClickFrameBtn = false;
                        binding.imageBtn.setChecked(false);
                    }
                } else {
                    Toast.makeText(HZKLDeviceMoreActivity.this, ((Business.RetObject) msg.obj).mMsg, Toast.LENGTH_SHORT).show();
                }
                isClickFrameBtn = true;
            }
        });
    }

    private void setFrameReverseStatus(final boolean isChecked) {
        binding.imageBtn.setClickable(false);
        String stats;
        if (isChecked) {
            stats = "reverse";
        } else {
            stats = "normal";
        }
        Business.getInstance().ModifyFrameReverse(deviceCode, "0", stats, new Handler() {
                    @Override
                    public void handleMessage(Message msg) {
                        if (msg.what == 0) {
                            Toast.makeText(HZKLDeviceMoreActivity.this, ((Business.RetObject) msg.obj).mMsg, Toast.LENGTH_SHORT).show();
                        } else {
                            isClickFrameBtn = false;
                            binding.imageBtn.setChecked(!isChecked);
                            Toast.makeText(HZKLDeviceMoreActivity.this, ((Business.RetObject) msg.obj).mMsg, Toast.LENGTH_SHORT).show();
                        }
                        isClickFrameBtn = true;
                        binding.imageBtn.setClickable(true);
                    }
                }
        );
    }
}
